package com.moonshineproject.plugin.actionscript.as3project.importer
{
	import com.moonshineproject.plugin.actionscript.as3project.vo.AS3ProjectVO;
	import com.moonshineproject.plugin.actionscript.as3project.vo.MXMLCConfigVO;
	import com.moonshineproject.plugin.core.importer.FlashDevelopImporterBase;
	import com.moonshineproject.utils.Utils;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	public class FlashDevelopImporter extends FlashDevelopImporterBase
	{
		public static function parse(file:File):AS3ProjectVO
		{
			var folder:File = file.parent;
			
			var p:AS3ProjectVO = new AS3ProjectVO(folder);
			p.projectFile = file;
			
			p.projectName = file.name.substring(0, file.name.lastIndexOf("."));
			p.config = new MXMLCConfigVO(folder.resolvePath("obj/"+p.projectName+"Config.xml"));
				
			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.READ);
			var data:XML = XML(stream.readUTFBytes(file.size));
			stream.close();
			
			// Parse XML file
			
			p.classpaths.length = 0;
			p.targets.length = 0;
			
			p.swfOutput.parse(data.output, p.folder);
			
			parsePaths(data.classpaths["class"], p.classpaths, p);
			
			p.buildOptions.parse(data.build);
			
			parsePaths(data.includeLibraries.element, p.includeLibraries, p);
			parsePaths(data.libraryPaths.element, p.libraries, p);
			parsePaths(data.externalLibraryPaths.element, p.externalLibraries, p);
			parsePaths(data.rslPaths.element, p.runtimeSharedLibraries, p);
			
			p.assetLibrary = data.library;
			parsePaths(data.compileTargets.compile, p.targets, p);
			parsePaths(data.hiddenPaths.hidden, p.hiddenPaths, p);
			
			p.prebuildCommands = Utils.deserializeString(data.preBuildCommand);
			p.postbuildCommands = Utils.deserializeString(data.postBuildCommand);
			p.postbuildAlways = Utils.deserializeBoolean(data.postBuildCommand.@alwaysRun);
			
			p.showHiddenPaths = Utils.deserializeBoolean(data.options.option.@showHiddenPaths);
			
			p.testMovie = data.options.option.@testMovie;
			if (p.testMovie == AS3ProjectVO.TEST_MOVIE_CUSTOM || p.testMovie == AS3ProjectVO.TEST_MOVIE_OPEN_DOCUMENT)
			{
				p.testMovieCommand = data.options.option.@testMovieCommand;
			}
			
			return p;
		}
		
		/*private static function parsePaths(paths:XMLList, v:Vector.<File>, p:AS3ProjectVO):void 
		{
			for each (var path:XML in paths) 
			{
				var f:File = p.folder.resolvePath(path.@path);
				f.canonicalize();
				v.push(f);
			}
		}*/

	}
}